package com.sztle.admodule.apiprocessor;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.sztle.admodule.GmAdManager;
import com.sztle.admodule.IShowListener;
import com.sztle.admodule.bean.AdReportInfo;
import com.sztle.admodule.databinding.ToastViewBinding;
import com.sztle.admodule.handler.RewardHandler;
import com.sztle.admodule.apiprocessor.listener.IOutRewardCallback;
import com.sztle.admodule.control.event.SzAdEvents;
import com.sztle.admodule.control.manager.ControlManager;
import com.sztle.admodule.control.utils.UserDeviceUtils;

public class OutRewardAdApi {

    public static void showRewardAd(Activity activity, String sceneName, IOutRewardCallback rewardCallback) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        if (!GmAdManager.isUseAdExp()) {
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        if(!GmAdManager.isAdSdkReady()){
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        Toast.makeText(activity,"加载中，请稍后...",Toast.LENGTH_SHORT).show();
        OutIntAdApi.cancelAdTimer();
        RewardHandler.loadAndShow(activity, new IShowListener() {
            @Override
            public void onFinalEnd(boolean result) {
                OutIntAdApi.startAdTimer();
                if (rewardCallback != null) {
                    rewardCallback.onRewardEnd();
                }

            }

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_reward, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    public static void showHomeRewardAd(Activity activity, String sceneName, IOutRewardCallback rewardCallback) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        ControlManager.triggerQueryAdjust();
        if(UserDeviceUtils.isGMore()){
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        if (!GmAdManager.isUseAdExp()) {
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        if(!GmAdManager.isAdSdkReady()){
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        if(ControlManager.getHomeRewardCount() <= 0){
            if (rewardCallback != null) {
                rewardCallback.onRewardEnd();
            }
            return;
        }
        Toast.makeText(activity,"加载中，请稍后...",Toast.LENGTH_SHORT).show();
        OutIntAdApi.cancelAdTimer();
        RewardHandler.loadAndShow(activity, new IShowListener() {
            @Override
            public void onFinalEnd(boolean result) {
                if(ControlManager.getHomeRewardCount() >= 2){
                    RewardHandler.loadAndShow(activity, new IShowListener() {
                        @Override
                        public void onAdShow(String ecpm) {
                            ControlManager.reportAdLog(SzAdEvents.show_reward, new AdReportInfo(ecpm, sceneName));
                        }

                        @Override
                        public void onFinalEnd(boolean result) {
                            OutIntAdApi.startAdTimer();
                            if (rewardCallback != null) {
                                rewardCallback.onRewardEnd();
                            }
                        }
                    });
                } else {
                    OutIntAdApi.startAdTimer();
                    if (rewardCallback != null) {
                        rewardCallback.onRewardEnd();
                    }
                }

            }

            @Override
            public void onAdShow(String ecmp) {
                ControlManager.reportAdLog(SzAdEvents.show_reward, new AdReportInfo(ecmp, sceneName));
            }

        });
    }

    private static Toast lastToast;

    private static void showToast(Activity activity, String msg) {
        if (activity == null || activity.isDestroyed()) {
            return;
        }
        if (lastToast != null) {
            lastToast.cancel();
            lastToast = null;
        }
        ToastViewBinding binding = ToastViewBinding.inflate(LayoutInflater.from(activity));
        Toast toast = new Toast(activity);
        toast.setView(binding.getRoot());
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        binding.tvToast.setText(msg);
        toast.show();
        lastToast = toast;
    }
}
